package com.dingup.provider.service.permission.impl;

import com.dingup.api.Util.RedisUtils;
import com.dingup.provider.cache.PermissionCache;
import com.dingup.provider.service.permission.PermissionCacheService;
import com.dingup.provider.service.sys.SysPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * Created by wancheng on 2018/9/5.
 */
@Service
public class PermissionCacheServiceImpl implements PermissionCacheService {

    @Autowired
    SysPermissionService sysPermissionService;

    @Override
    public Boolean getPermission(String userAccount, String filed) {
        Boolean res = null;
        try {
             res = PermissionCache.getInstance().get(userAccount,filed);
        } catch (Exception e) {
            e.printStackTrace();
            String userId = RedisUtils.getInstance().getUserId(userAccount);
            Map<String,Boolean> result = sysPermissionService.getRoleHasPermissionGroupTreeByUserId(userId);
            setPermission(userAccount,result);
            res = result.get(filed);
        }
        return res;
    }

    @Override
    public void setPermission(String userAccount, Map<String, Boolean> value) {
        PermissionCache.getInstance().put(userAccount,value);
    }
}
